/**
 * Add or Edit Page
 */
var StorageStockInfoDlg = {
    data: {
        operateId: "",
        operateName: "",
        customerId: "",
        customerName: "",
        warehouseId: "",
        warehouseName: "",
        areaId: "",
        areaNumber: "",
        positionId: "",
        positionNumber: "",
        trayId: "",
        trayNumber: "",
        goodsId: "",
        goodsName: "",
        goodsNumber: "",
        num: "",
        balenum: "",
        canNum: "",
        stockWeight: "",
        stockVolume: "",
        unitprice: "",
        stockMoney: "",
        deliverId: "",
        deptId: "",
        version: "",
        createTime: "",
        updateTime: "",
        createUser: "",
        updateUser: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    //Auto adjust the height of current iframe layer
    admin.iframeAuto();

     //Render date
    laydate.render({
        elem: '#producetime' //Production date
    });

    //Render date
    laydate.render({
        elem: '#putTime' //Production date
    });

    //Query storage areas
    form.on('select(warehouseId)',function(){
        $("#areaId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicArea/Querylist',
            dataType:"json",   //Return format as json
            async: false,
            data:{"areaWarehouseId":$("#warehouseId").val()},    //Parameter value
            type:"post",   //Request method
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Please select</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.areaNumber+"</option>";
                    });
                    $("#areaId").html(str);
                }
            }
        });
        form.render('select');
    });

    //Query storage locations
    form.on('select(areaId)',function(){
        $("#positionId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicPosition/Querylist',
            dataType:"json",   //Return format as json
            async: false,
            data:{"positionAreaId":$("#areaId").val()},    //Parameter value
            type:"post",   //Request method
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Please select</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.positionNumber+"</option>";
                    });
                    $("#positionId").html(str);
                }
            }
        });
        form.render('select');
    });

    //Click on goods name
    $("#goodsName").click(function(){
        layer.open({
            type: 2,
            title: 'Select Goods',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/storageStock/toChoiceGoods?goodsCustomerId=' + $("#customerId").val(),
            end: function () {
                $("#goodsId").val(StorageStockInfoDlg.data.goodsId);//Goods id
                $("#goodsName").val(StorageStockInfoDlg.data.goodsName);//Goods name
                $("#goodsNumber").val(StorageStockInfoDlg.data.goodsNumber);//Goods number
            }
        });
    });

    //Form submission event
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/storageStock/addItem", function (data) {
        	if (data.success){
	            Feng.success("Added successfully!");
	            window.location.href = Feng.ctxPath + '/storageStock/toStockList'
        	} else {
            	Feng.error("Failed to add!" + data.message)
        	}
            Feng.success("Added successfully!");
        }, function (data) {
            Feng.error("Failed to add!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //Back button
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/storageStock/toStockList'
    });

});